package com.anyonavinfo.cpaddemo.view.mainview;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import com.anyonavinfo.cpaddemo.R;
import com.anyonavinfo.cpaddemo.data.appinfo.source.AppInfoRepository;

/**
 * Created by ZuiC on 2017/1/16.
 */

public class MainView extends RelativeLayout implements MainViewContract.View {

    private static final String TAG = "OuterFrameView";

    private Context mContext;
    private ImageButton imgBtnOpenASR;
    private MainViewPager mMainViewPager;
    private LinearLayout lvDotIcon;
    private ImageView mCurSelectedDot,mPreSelectedDot;
    private int mCurSelectedPage;
    private MainViewContract.Presenter mPresenter;
    private int mPageSize;

    public MainView(Context context) {
        super(context);
        initView(context);
        setListener();
    }

    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        setListener();
    }

    public MainView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        setListener();
    }


    private void initView(Context context){
        this.mContext = context;
        mCurSelectedDot = null;
        mPreSelectedDot = null;
        mCurSelectedPage = 0;
        mPageSize = 0;

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.view_mainview_mainview, this);
        imgBtnOpenASR = (ImageButton) view.findViewById(R.id.openasr_imgbtn);
        mMainViewPager = (MainViewPager) view.findViewById(R.id.viewpager);
        lvDotIcon = (LinearLayout) view.findViewById(R.id.dot_icon_lv);
        new MainViewPresenter(MainView.this, AppInfoRepository.getInstance()).start();
    }

    private void setListener(){
        mMainViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mPresenter.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        imgBtnOpenASR.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onImgBtnOpenASRClick();
            }
        });
    }

    private void onImgBtnOpenASRClick(){
        mPresenter.openASRView();
    }

    public boolean onBackPressed(){
        return mMainViewPager.onBackPressed();
    }

<<<<<<< HEAD:app/src/main/java/com/anyonavinfo/cpaddemo/view/mainview/MainView.java
    public void onRedisplay(){
        mMainViewPager.onRedisplay();
    }

=======
>>>>>>> b56716b621890f59100a220d4c7abe0ddb29e232:app/src/main/java/com/anyonavinfo/cpaddemo/view/mainview/MainView.java
    public void onResume(boolean reloadAppInfo){
        if (reloadAppInfo){
            mPresenter.loadAppInfo(reloadAppInfo);
        }
    }

    @Override
    public void updateDotIconState(int position) {
        mCurSelectedPage = position;
        if (mPreSelectedDot != null){
            mPreSelectedDot.setImageResource(R.drawable.icon_dot_unselected);
        }
        mCurSelectedDot = ((ImageView)lvDotIcon.getChildAt(mCurSelectedPage));
        mCurSelectedDot.setImageResource(R.drawable.icon_dot_selected);
        mPreSelectedDot = mCurSelectedDot;
    }

    @Override
    public void updateDotIconNum() {
        lvDotIcon.removeAllViews();
        for (int i = 0; i < mPageSize; i++){
            ImageView dot_icon = new ImageView(mContext);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                    , ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i < mPageSize - 1){
                layoutParams.setMargins(0, 0, 20, 0);
            }
            dot_icon.setLayoutParams(layoutParams);
            dot_icon.setImageResource(R.drawable.icon_dot_unselected);
            lvDotIcon.addView(dot_icon);
        }
    }

    @Override
    public void updatePageSize(int size) {
        mPageSize = size;
        mMainViewPager.updatePageSize(size);
    }

    @Override
    public void setPresenter(MainViewContract.Presenter presenter) {
        mPresenter = presenter;
    }
}
